package net.sunofbeach.blog.utils;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Random;

@Slf4j
public class UploadPdfToCosUtils {


    public static String uploadPdf(MultipartFile file) {

        // 1 初始化用户身份信息(secretId, secretKey，可在腾讯云后台中的API密钥管理中查看！
        COSCredentials cred = new BasicCOSCredentials(Constants.COS.ACCESS_KEY_ID, Constants.COS.ACCESS_KEY_SECRET);

        // 2 设置bucket的区域, COS地域的简称请参照
        // https://cloud.tencent.com/document/product/436/6224，根据自己创建的存储桶选择地区
        ClientConfig clientConfig = new ClientConfig(new Region(Constants.COS.SAVE_AREA));
        COSClient cosClient = new COSClient(cred, clientConfig);
        String fileName = file.getName();
        try {
            //获得文件类型
            String substring = file.getContentType();
            //获得文件名
            String originalFilename = file.getOriginalFilename();
            //获得文件类型
            String type = getType(substring, originalFilename);
            log.info(substring);
            //获得文件类型
            Random random = new Random();
            //命名
            fileName = "bg/" + random.nextInt(10000) + System.currentTimeMillis()+ "." + type;
            //必须设置文件格式，不然打开地址就是直接下载 不可以预览
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType(substring);
            InputStream in = file.getInputStream();
            PutObjectRequest putObjectRequest = new PutObjectRequest(Constants.COS.BUCKET_NAME, fileName, in, meta);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭客户端(关闭后台线程)
            cosClient.shutdown();
        }
        return fileName;

    }

    public static String getType(String contentType, String name) {
        String type = null;
        if (Constants.ImageType.TYPE_PNG_WITH_PREFIX.equals(contentType)
                && name.endsWith(Constants.ImageType.TYPE_PNG)) {
            type = Constants.ImageType.TYPE_PNG;
        } else if (Constants.ImageType.TYPE_GIF_WITH_PREFIX.equals(contentType)
                && name.endsWith(Constants.ImageType.TYPE_GIF)) {
            type = Constants.ImageType.TYPE_GIF;
        } else if (Constants.ImageType.TYPE_JPG_WITH_PREFIX.equals(contentType)
                && name.endsWith(Constants.ImageType.TYPE_JPG)) {
            type = Constants.ImageType.TYPE_JPG;
            //添加一个新的pdf文件
        }else if (Constants.PdfType.TYPE_PDF_WITH_PREFIX.equals(contentType)
                && name.endsWith(Constants.PdfType.TYPE_PDF)) {
            type = Constants.PdfType.TYPE_PDF;
        }
        return type;
    }

}
